package edu.neusoft.yuejian_09.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.TrainInfoActivity;
import edu.neusoft.yuejian_09.bean.Train;
import it.sephiroth.android.library.picasso.Picasso;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

/**
 * Created by 爽 on 2017/10/12.
 */

public class TrainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater1;

    public TrainListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater1 = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Train entity = (Train) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.train_name.setText(entity.getTrain_name());
        viewHolder.club_name.setText(entity.getClub_name());
        System.out.printf("train_name");
        viewHolder.price.setText("￥"+entity.getTrain_price()+ "元");
        String train_pic =PHOTO_URL+entity.getTrain_pic();

        Picasso.with(mContext).load(train_pic).into(viewHolder.train_pic);
        if(viewHolder.train_pic.getDrawable()==null){
            viewHolder.train_pic.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("train_id",entity.getTrain_id());
                bundle.putInt("club_id",entity.getClub_id());
                bundle.putString("train_name",entity.getTrain_name());
                bundle.putInt("train_price",entity.getTrain_price());
                bundle.putString("train_pic",entity.getTrain_pic());
                Intent intent=new Intent(mContext, TrainInfoActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        View v = mLayoutInflater1.from(parent.getContext()).inflate(R.layout.cardview_train, parent, false);
        return new ViewHolder(v);

    }

    //自定义的V
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView train_pic;
        TextView train_name,price,club_name;

        public ViewHolder(View itemView) {
            super(itemView);
            train_name = (TextView) itemView.findViewById(R.id.Train_name);
            train_pic = (ImageView) itemView.findViewById(R.id.Trainlist_pic);
            club_name= (TextView) itemView.findViewById(R.id.club_name);
            price= (TextView) itemView.findViewById(R.id.price);
        }
    }
}
